/**=========================================================
 * Module: config.js
 * App routes and resources configuration
 =========================================================*/

App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider',
    function ($stateProvider, $locationProvider, $urlRouterProvider, helper) {
        'use strict';

        // Set the following to true to enable the HTML5 Mode
        // You may have to set <base> tag in index and a routing configuration in your server
        $locationProvider.html5Mode(false);

        $urlRouterProvider.otherwise('/app/loading');
        
        // 
        // Application Routes
        // -----------------------------------       
        $stateProvider
        // 
        // Single Page Routes
        // ----------------------------------- 
        .state('page', {
            url: '/page',
            templateUrl: 'app/pages/page.html',
            resolve: helper.resolveFor('modernizr', 'icons'),
            controller: ["$rootScope", function($rootScope) {
                $rootScope.app.layout.isBoxed = false;
            }]
        })
        .state('page.login', {
            url: '/login',
            title: "Login",
            templateUrl: 'app/pages/login.html' 
        })
        .state('page.404', {
            url: '/404',
            title: "Not Found",
            templateUrl: 'app/pages/404.html'
        })

        .state('app', {
            url: '/app',
            abstract: true,
            templateUrl: helper.basepath('app.html'),
            controller: 'AppController',
            resolve: helper.resolveFor('modernizr', 'icons', 'screenfull', 'ui.grid', 'ui.select', 'ngDialog','toaster','whirl')
        })
        .state('app.loading', {
            url: '/loading',
            template: ''
        })
        .state('app.system-menu', {
            url: '/system/menu',
            templateUrl: helper.basepath('system/menu.html'),
            resolve: helper.resolveFor('ztree')
        })
        .state('app.system-user', {
            url: '/system/user',
            templateUrl: helper.basepath('system/user.html')
        })
        .state('app.system-role', {
            url: '/system/role',
            templateUrl: helper.basepath('system/role.html'),
            resolve: helper.resolveFor('ztree', 'ladda')
        })
        .state('app.system-group', {
            url: '/system/group',
            templateUrl: helper.basepath('system/group.html')
        })
        .state('app.system-job', {
            url: '/system/job',
            templateUrl: helper.basepath('system/job.html')
        })
        .state('app.system-task', {
            url: '/system/task',
            templateUrl: helper.basepath('system/task.html')
        })
        .state('app.system-updateLog', {
            url: '/system/updateLog',
            templateUrl: helper.basepath('system/updateLog.html')
        })
        .state('app.system-updateLogView', {
            url: '/system/updateLogView',
            templateUrl: helper.basepath('system/updateLogView.html')
        })
        .state('app.system-cache', {
            url: '/system/cache',
            templateUrl: helper.basepath('system/cache.html')
        })
        .state('app.system-dict', {
            url: '/system/dict',
            templateUrl: helper.basepath('system/dict.html')
        })
        .state('app.system-commonQueryUrl', {
            url: '/system/commonQueryUrl',
            templateUrl: helper.basepath('system/commonQueryUrl.html')
        })
    }
]).config(['$ocLazyLoadProvider', 'APP_REQUIRES', 
    function ($ocLazyLoadProvider, APP_REQUIRES) {
        'use strict';

        // Lazy Load modules configuration
        $ocLazyLoadProvider.config({
        debug: false,
        events: true,
        modules: APP_REQUIRES.modules
        });

    }
]).config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide',
    function ( $controllerProvider, $compileProvider, $filterProvider, $provide) {
        'use strict';
        // registering components after bootstrap
        App.controller = $controllerProvider.register;
        App.directive  = $compileProvider.directive;
        App.filter     = $filterProvider.register;
        App.factory    = $provide.factory;
        App.service    = $provide.service;
        App.constant   = $provide.constant;
        App.value      = $provide.value;

    }
]).config(['$translateProvider', 'tmhDynamicLocaleProvider',
    function ($translateProvider, tmhDynamicLocaleProvider) {
        var language = localStorage.NG_TRANSLATE_LANG_KEY ? localStorage.NG_TRANSLATE_LANG_KEY : 'zh';
        $translateProvider.useStaticFilesLoader({
            prefix : 'app/i18n/',
            suffix : '.json?version=' + version
        });
        $translateProvider.preferredLanguage(language);
        $translateProvider.useLocalStorage();
        $translateProvider.usePostCompiling(true);
        
        tmhDynamicLocaleProvider.localeLocationPattern('vendor/angular-i18n/angular-locale_{{locale}}.js');
        tmhDynamicLocaleProvider.defaultLocale(language);
    }
]).config(['cfpLoadingBarProvider', 
    function(cfpLoadingBarProvider) {
        cfpLoadingBarProvider.includeBar = true;
        cfpLoadingBarProvider.includeSpinner = false;
        cfpLoadingBarProvider.latencyThreshold = 500;
        cfpLoadingBarProvider.parentSelector = '.wrapper > section';
    }
]).config(['$uibTooltipProvider', 
    function ($tooltipProvider) {
        $tooltipProvider.options({appendToBody: true});
    }
]).config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]).config(function($provide){
    $provide.decorator('GridOptions',['$delegate', 'i18nService', function($delegate, i18nService){
        var gridOptions;
        gridOptions = angular.copy($delegate);
        gridOptions.initialize = function(options) {
            var initOptions;
            initOptions = $delegate.initialize(options);
            return initOptions;
        };
        
        var language = localStorage.NG_TRANSLATE_LANG_KEY == 'zh' ? 'zh-cn' : localStorage.NG_TRANSLATE_LANG_KEY || 'zh-cn';

        i18nService.setCurrentLang(language);
        return gridOptions;
    }]);
});